package com.knowswift.issues.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @Description:时间工具类，支持JDK8，兼容JDK6
 * @Author:SYC
 * @Since:2016年9月18日
 * @Version:1.1.0
 */
public final class DateUtil {

    /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */
    public static final String FULL_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /** 日期格式yyyy-MM-dd字符串常量 */
    public static final String SIMPLE_PATTERN = "yyyy-MM-dd";
    /** 日期格式yyyyMMdd字符串常量 */
    public static final String SIMPLE_PATTERN_3 = "yyyyMMdd";
    /** 日期格式HH:mm:ss字符串常量 */
    private static final String HOUR_FORMAT = "HH:mm:ss";
    /** 日期格式HHmmss字符串常量 */
    private static final String HOUR_FORMAT_2 = "HHmmss";
    /** 日期格式yyyyMMddHHmmss字符串常量 */
    public static final String FULL_PATTERN2 = "yyyyMMddHHmmss";
    
    private static final String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT);
    private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(FULL_PATTERN);
    private static SimpleDateFormat sdf_datetime_format2 = new SimpleDateFormat(FULL_PATTERN2);
    private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(SIMPLE_PATTERN);
    private static SimpleDateFormat sdf_date_format3 = new SimpleDateFormat(SIMPLE_PATTERN_3);
    private static SimpleDateFormat sdf_hour_format2 = new SimpleDateFormat(HOUR_FORMAT_2);

    private DateUtil() {
    }

    /**
     * 通过秒数得到时间
     *
     * @param seconds
     *            秒数
     * @return 时间对象
     */
    public static String fromUTCtoFull(String dateUtc) {
    	return formateDate(toDateTime(dateUtc,STANDARD_DATE_FORMAT_UTC));
    }
    /**
     * 通过秒数得到时间
     *
     * @param seconds
     *            秒数
     * @return 时间对象
     */
    public static Date secondsToDate(long seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(seconds * 1000);
        return calendar.getTime();
    }

    /**
     * 通过秒数得到时间
     *
     * @param seconds
     *            秒数
     * @return 时间字符串
     */
    public static String secondsToDateStr(long seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(seconds * 1000);
        return sdf_datetime_format.format(calendar.getTime());
    }

    /**
     * 
     * @param seconds
     *            秒数
     * @param pattern
     *            时间字符串
     * @return
     * @Description:
     */
    public static String secondsToDateStrByPattern(long seconds, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(seconds * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(calendar.getTime());
    }

    /**
     * 格式化时间对象
     *
     * @param date
     *            时间对象
     * @param pattern
     *            格式化正则
     * @return 时间字符串
     */
    public static String formateDate(Date date, String pattern) {
        String str = "";
        if (null != date) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            str = formatter.format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("+8")));
        }
        return str;
    }

    /**
     * 格式化时间对象，使用yyyy-MM-dd HH:mm:ss格式
     *
     * @param date
     *            时间对象
     * @return 时间字符串
     */
    public static String formateDate(Date date) {
        return formateDate(date, FULL_PATTERN);
    }

    /**
     * 格式化时间对象，使用yyyy-MM-dd HH:mm:ss格式
     *
     * @param date
     *            时间对象
     * @return 时间字符串
     */
    public static String formateDate(Long dateTimes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(dateTimes);
        return formateDate(calendar.getTime(), FULL_PATTERN);
    }
    
    public static String formatDate(String date, String pattern){
    	Date d=toDateTime(date,pattern);
    	return formateDate(d,pattern);
    }
    
    public static String formatDate(String date){
    	Date d=toDateTime(date,FULL_PATTERN);
    	return formateDate(d,FULL_PATTERN);
    }

    /**
     * 时间格式字符串转换成时间对象，使用yyyy-MM-dd HH:mm:ss格式
     *
     * @param dateStr
     *            时间字符串
     * @return 时间对象
     */
    public static Date StrtoDateTime(String dateStr) {
        return toDateTime(dateStr, FULL_PATTERN);
    }

    /**
     * 时间格式字符串转换成时间对象
     *
     * @param dateStr
     *            时间字符串
     * @param pattern
     *            格式化正则
     * @return 时间对象
     */
    public static Date toDateTime(String dateStr, String pattern) {
        Date date = null;
        DateFormat format = new SimpleDateFormat(pattern);
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
        }
        return date;
    }

    public static long strDateToLong(String dateStr) {
        DateFormat format = new SimpleDateFormat(SIMPLE_PATTERN);
        long time = 0;
        try {
            Date dt = format.parse(dateStr);
            time = dt.getTime();
        } catch (ParseException e) {
 
        }
        return time;
    }

    public static long strDateTimeToLong(String dateStr) {
        DateFormat format = new SimpleDateFormat(FULL_PATTERN);
        long time = 0;
        try {
            Date dt = format.parse(dateStr);
            time = dt.getTime();
        } catch (ParseException e) {

        }
        return time;
    }

    public static long strDateTimeToLong(String dateStr,String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        long time = 0;
        try {
            Date dt = format.parse(dateStr);
            time = dt.getTime();
        } catch (ParseException e) {

        }
        return time;
    }
    
    
    
    /**
     * 
     * @return
     * @Description:获得服务器当前日期及时间，以格式为：yyyy-MM-dd HH:mm:ss的日期字符串形式返回
     */
    public static String getDateTime() {
        try {
            return sdf_datetime_format.format(Calendar.getInstance().getTime());
        } catch (Exception e) {

            return "";
        }
    }

    /**
     * 
     * @return
     * @Description:获得服务器当前日期及时间，以格式为：yyyyMMddHHmmss的日期字符串形式返回
     */
    public static String getDateTimeForFormat2() {
        try {
            return sdf_datetime_format2.format(Calendar.getInstance().getTime());
        } catch (Exception e) {

            return "";
        }
    }

    /**
     * 
     * @return
     * @Description:获得服务器当前日期，以格式为：yyyy-MM-dd的日期字符串形式返回
     */
    public static String getDate() {
        try {
            return sdf_date_format.format(Calendar.getInstance().getTime());
        } catch (Exception e) {

            return "";
        }
    }

    /**
     * 
     * @return
     * @Description:获得服务器当前时间，以格式为：HH:mm:ss的日期字符串形式返回
     */
    public static String getTime() {
        String temp = " ";
        try {
            temp += sdf_hour_format.format(Calendar.getInstance().getTime());
            return temp;
        } catch (Exception e) {

            return "";
        }
    }

    public static String dateToDateFormat(Date date) {
        try {
            return sdf_date_format3.format(date);
        } catch (Exception e) {

            return "";
        }
    }

    public static String dateTOHourFormat(Date date) {
        try {
            return sdf_hour_format2.format(date);
        } catch (Exception e) {

            return "";
        }
    }

    public static String dateFormat(Date date, String regex) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(regex);
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 根据生日计算宝宝总月数
     *
     * @param birthday
     *            生日long类型
     * @return 总月数
     */
    public static int getMonthByBirthday(long brithday) throws ParseException, Exception {
        try {
            long currentTime = System.currentTimeMillis();
            long days = currentTime / (24 * 60 * 60 * 1000) - brithday / (24 * 60 * 60);
            Long month = days / 30;
            if (month < 1) {
                return 0;
            }
            return month.intValue();
        } catch (Exception e) {
            return 0;
        }
    }

    // 总月数计算测试
    /*public static void main(String[] args) throws ParseException, Exception {
        // 2014/6/28 12:56:07 2016/10/18 17:07 28
        // 2012 8/1 2016/10/18 17:07 51
        System.out.println(System.currentTimeMillis());
        System.out.println(fromUTCtoFull("2018-04-25T20:02:45.803+08:00"));
        System.out.println(secondsToDateStrByPattern(1450972800, FULL_PATTERN));
        System.out.println(getMonthByBirthday(1450972800));
        
        
        System.out.println("kkk"+formatDate("1900-01-01 00:00:00.0"));
    }*/
}
